package com.deyuanyun.pic.eqpt.service.impl;

import com.deyuanyun.pic.common.util.MapUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.eqpt.domain.ExtemalUse;
import com.deyuanyun.pic.eqpt.mapper.EquipmentInfoMapper;
import com.deyuanyun.pic.eqpt.mapper.ExtemalUseMapper;
import com.deyuanyun.pic.eqpt.service.ExtemalUseService;
import com.deyuanyun.pic.settings.support.biz.CodeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Bill.li on 2015/12/30.
 */
@Service
public class ExtemalUseServiceImpl implements ExtemalUseService{
    @Resource
    private ExtemalUseMapper extemalUseMapper;
    @Resource
    private EquipmentInfoMapper equipmentInfoMapper;//添加借出时会清空设备的状态


    @Override
    public List<ExtemalUse> getExternally(HashMap<String,Object> extemalUse) {
        return extemalUseMapper.getExternally(extemalUse);//外部借用页面数据和条件查询
    }

    @Override
    public Boolean addExternally(HashMap param) throws Exception{
        List<String> error=new ArrayList<String>();
        String id= UUID.randomUUID().toString().replaceAll("-", "");
        ExtemalUse info= (ExtemalUse) MapUtil.mapToObject(param, ExtemalUse.class);
        info.setId(id);
        
        if(StringUtils.isEmpty(info.getEqptEquipmentId())){
            error.add("设备ID不能为空!");
            param.put("errList", "设备ID不能为空");
            return false;
        }
        if(StringUtils.isEmpty(info.getUseCompany())){
            error.add("公司名字不能为空!");
            param.put("errList", "公司名字不能为空");
            return false;
        }
        if(StringUtils.isEmpty(info.getUseUser())){
            error.add("领用人不能为空!");
            param.put("errList", "领用人不能为空");
            return false;
        }
        if(StringUtils.isEmpty(info.getStartDate().toString())){
            error.add("领用日期不能为空");
            param.put("errList", "领用日期不能为空");
            return false;
        }
        if(StringUtils.isEmpty(info.getPlanEndDateStr().toString())){
            error.add("归还日期不能为空");
            param.put("errList", "归还日期不能为空");
            return false;
        }
        if(StringUtils.isEmpty(info.getMobile())){
            error.add("领用人联系电话不能为空!");
            param.put("errList", "领用人联系电话不能为空");
            return false;
        }
        info.setPlanEndDate(new SimpleDateFormat("yyyy-MM-dd").parse(info.getPlanEndDateStr()));
        param.remove(id);//判断id是否存在

        extemalUseMapper.addExternally(info);
        
        //将借出设备更新状态为服务中 
        Map<String,String>  equipmentMap=new HashMap<String,String> ();
        equipmentMap.put("id", info.getEqptEquipmentId());
        equipmentMap.put("cstate", CodeUtil.getCodeId("EQPT_STATE", "服务中"));
        equipmentInfoMapper.updataLoan(equipmentMap);
        return true;//添加外部借用

    }

    @Override
    public Boolean updateExternally(HashMap param)throws Exception{
        List<String> error=new ArrayList<String>();
 
        ExtemalUse info= null;
        try {
            info = (ExtemalUse) MapUtil.mapToObject(param, ExtemalUse.class);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        if(StringUtils.isEmpty(info.getEqptEquipmentId())){
            error.add("设备ID不能为空!");
            param.put("errList", "设备ID不能为空");
            return false;
        }
        if(StringUtils.isEmpty(info.getUseCompany())){
            error.add("公司名字不能为空!");
            param.put("errList", "公司名字不能为空");
            return false;
        }
        if(StringUtils.isEmpty(info.getUseUser())){
            error.add("领用人不能为空!");
            param.put("errList", "领用人不能为空");
            return false;
        }
        if(StringUtils.isEmpty(info.getStartDate().toString())){
            error.add("领用日期不能为空");
            param.put("errList", "领用日期不能为空");
            return false;
        }
        if(StringUtils.isEmpty(info.getPlanEndDateStr().toString())){
            error.add("归还日期不能为空");
            param.put("errList", "归还日期不能为空");
            return false;
        }
        if(StringUtils.isEmpty(info.getMobile())){
            error.add("领用人联系电话不能为空!");
            param.put("errList", "领用人联系电话不能为空");
            return false;
        }
        
        info.setPlanEndDate(new SimpleDateFormat("yyyy-MM-dd").parse(info.getPlanEndDateStr()));
        //将原借出设备更新状态为闲置中 
        Map<String,String>  equipmentMap=new HashMap<String,String> ();
        equipmentMap.put("id", extemalUseMapper.findEquipment(info.getId()));
        equipmentMap.put("cstate", CodeUtil.getCodeId("EQPT_STATE", "闲置中"));
        equipmentInfoMapper.updataLoan(equipmentMap);
        
        extemalUseMapper.updateByPrimaryKeySelective(info);
        
        //将借出设备更新状态为服务中 
        equipmentMap=new HashMap<String,String> ();
        equipmentMap.put("id", info.getEqptEquipmentId());
        equipmentMap.put("cstate", CodeUtil.getCodeId("EQPT_STATE", "服务中"));
        equipmentInfoMapper.updataLoan(equipmentMap);
        return true;//更新外部借用
    }

    @Override
    public boolean delExternally(HashMap<String, Object> extemalUse) {
    	String id=(String) extemalUse.get("id");
    	if(!StringUtils.isEmpty(id))
    	{
    		//将原借出设备更新状态为闲置中 
            Map<String,String>  equipmentMap=new HashMap<String,String> ();
            equipmentMap.put("id", extemalUseMapper.findEquipment(id));
            equipmentMap.put("cstate", CodeUtil.getCodeId("EQPT_STATE", "闲置中"));
            equipmentInfoMapper.updataLoan(equipmentMap);
    	}
        return extemalUseMapper.delExternally(extemalUse);//删除外部借用(包含多个删除)
    }

	@Override
	public List<Map> getTask(HashMap<String, Object> pram) {
		// TODO Auto-generated method stub
		return extemalUseMapper.getTask(pram);
	}
}
